package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketKeywordService;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketKeywordServiceImpl implements MarketKeywordService {
    @Autowired
    private MarketKeywordMapper marketKeywordMapper;

    @Override
    public List<MarketKeyword> list(String keyword, int page, int limit, String sort, String order) {
        
        
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        
        
        return marketKeywords;
    }

    @Override
    public MarketKeyword create(String keyword, boolean isHot, boolean isDefault) {
        
        
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keyword);
        LocalDateTime dateTime = LocalDateTime.now();
        marketKeyword.setAddTime(dateTime);
        marketKeyword.setUpdateTime(dateTime);
        marketKeyword.setDeleted(false);
        marketKeyword.setUrl("");
        marketKeyword.setSortOrder(100);
        marketKeyword.setIsDefault(isDefault);
        marketKeyword.setIsHot(isHot);
        marketKeywordMapper.insert(marketKeyword);
        
        
        return marketKeyword;
    }

    @Override
    public MarketKeyword update(int id, String keyword, String url, boolean isDefault, boolean isHot) {
        
        
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.or().andIdEqualTo(id);
        MarketKeyword marketKeyword = marketKeywordMapper.selectByExample(keywordExample).get(0);
        LocalDateTime dateTime = LocalDateTime.now();
        marketKeyword.setUpdateTime(dateTime);
        marketKeyword.setKeyword(keyword);
        marketKeyword.setIsHot(isHot);
        marketKeyword.setIsDefault(isDefault);
        marketKeyword.setUrl(url);
        marketKeywordMapper.updateByExample(marketKeyword, keywordExample);
        
        
        return marketKeyword;
    }

    @Override
    public void delete(int id) {
        
        
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.or().andIdEqualTo(id);
        marketKeywordMapper.logicalDeleteByExample(keywordExample);
        
        
    }


}
